package com.javaweb.bookstore.servlet;

import com.javaweb.bookstore.*;
import com.javaweb.bookstore.component.IBookManager;
import com.javaweb.bookstore.component.JdbcBookManager;
import com.javaweb.bookstore.entity.Book;
import com.javaweb.bookstore.entity.ResponseEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/browse/book")
public class BrowseServlet extends HttpServlet {
    IBookManager manager;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       String key =  req.getParameter("key");

            // 序列化为JSON字符串
            List<Book> books = manager.getBooks(key);
            ResponseEntity re = new ResponseEntity(true, "browse success!", books);
            String jsonString = Utils.obj2json(re);
            System.out.println("序列化结果: " + jsonString);
            resp.getWriter().println(jsonString);
            return;



    }

}
